package com.example.tossednfound;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class UserListActivity extends ListActivity {

	int action = 0;
	RegUser selectedUser;
	ArrayAdapter<RegUser> adapter;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_all_users);
		adapter = new ArrayAdapter<RegUser>(this,
				R.layout.activity_all_users, R.id.user_listView,
				LoginActivity.UserList);
		setListAdapter(adapter);
		ListView listView = getListView();
		listView.setTextFilterEnabled(true);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedUser = LoginActivity.UserList.get(position);
				if (action == 1) { 
					
					Toast.makeText(
							getApplicationContext(),
							LoginActivity.UserList.get(position).email
									+ " has been removed.", Toast.LENGTH_SHORT)
							.show();
					AdminUser.removeUser(selectedUser);
					adapter.remove(selectedUser);
					adapter.notifyDataSetChanged();
				} else if (action == 2) {
					selectedUser.isLocked = !selectedUser.isLocked;
					selectedUser.loginCount = 0;
						Toast.makeText(
								getApplicationContext(), 
								selectedUser.email
										+ " is locked: "
										+ selectedUser.isLocked,
								Toast.LENGTH_SHORT).show();
						adapter.notifyDataSetChanged();
					
				} else if (action == 3) {
					
					Toast.makeText(
							getApplicationContext(),
							LoginActivity.UserList.get(position).email
									+ " has been made an admin.",
							Toast.LENGTH_SHORT).show();
					
					AdminUser newAdmin = new AdminUser(selectedUser);
					adapter.remove(selectedUser);
					LoginActivity.UserList.remove(selectedUser);
					LoginActivity.AdminList.add(newAdmin);
					adapter.notifyDataSetChanged();
					
				} else {
					Toast.makeText(getApplicationContext(),
							"Locked: " + LoginActivity.UserList.get(position).isLocked,
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		listView.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				Toast.makeText(getApplicationContext(), "LONG CLICK",
						Toast.LENGTH_SHORT).show();
				return false;
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.activity_admin, menu);
		return true;
	}
/*
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;

		menu.setHeaderTitle("Options for "
				+ LoginActivity.UserList.get(aInfo.position).email);
		menu.add(1, 1, 1, "Delete");
		menu.add(1, 2, 2, "Make Admin");
		menu.add(1, 3, 3, "Unlock/Lock");
		menu.add(action, action, action, null);
	}
*/
	/*@Override
	public boolean onContextItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		int position = LoginActivity.UserList.indexOf(selectedUser);
		if (itemId == 1) {
			try {
				Toast.makeText(
						getApplicationContext(),
						LoginActivity.UserList.get(position).email
								+ " has been removed.", Toast.LENGTH_SHORT)
						.show();
				AdminUser.removeUser(selectedUser);
			} catch (Exception e) {
				return false;
			}
		} else if (itemId == 2) {

			try {
				Toast.makeText(
						getApplicationContext(),
						"Before is locked: "
								+ LoginActivity.UserList.get(position).isLocked,
						Toast.LENGTH_SHORT).show();
				AdminUser.toggleLock(selectedUser);
				Toast.makeText(
						getApplicationContext(),
						LoginActivity.UserList.get(position).email
								+ " is locked: "
								+ LoginActivity.UserList.get(position).isLocked,
						Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				return false;
			}

		} else if (itemId == 3) {
			try {
				Toast.makeText(
						getApplicationContext(),
						LoginActivity.UserList.get(position).email
								+ " has been made an admin.",
						Toast.LENGTH_SHORT).show();
				AdminUser.makeAdmin(selectedUser);
			} catch (Exception e) {
				return false;
			}
		}
		return true;
	}*/

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Intent adminHomeActivity = new Intent(
				"android.intent.action.ADMINHOMEACTIVITY");
		startActivity(adminHomeActivity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int selected = item.getItemId();
		if (selected == R.id.menu_delete_user) {
			action = 1;
		} else if (selected == R.id.menu_toggle_lock) {
			action = 2;
		} else if (selected == R.id.menu_make_admin) {
			action = 3;
		}
		return super.onOptionsItemSelected(item);
	}

	
}
